package com.base.myproject.client.busi;

import java.util.List;

import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseAdvSearch;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;

public class Testbusi3 {
	
	  ModelType type  = null;
	  List<ColumnConfig> configs =null;
	  StoreInfo info = null;
	  BaseStore store = null;
	public Testbusi3(List<ColumnConfig> configs , ModelType type ,StoreInfo info ) {

		this.type = type;
		
		this.configs = configs;
		this.info = info;
	}

	public ContentPanel init() {
		
		
		
		/*List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId("cname");
		column.setHeader("Company");
		column.setWidth(200);
		configs.add(column);
		
		   type = new ModelType();  
		    type.root = "records";  
		    type.recordName = "record";  
		    type.totalName = "totle";  
		    type.addField("cname", "cname");  
		// BasePagingLoader loader = getBaseLoader();
		
		 StoreInfo info= new StoreInfo();
		    info.setTablename("ccode");
		    info.setSql("select * from ccode");
		 */
		 ColumnModel cm = new ColumnModel(configs);  
		 store = new BaseStore();  
		// loader.load();
		 store.setType(type);
		 store.setStoreinfo(info);
		 PagingToolBar toolBar = new PagingToolBar(50);  
		  //  toolBar.bind( (BasePagingLoader)store.getLoader());  
		    store.setToolBar(toolBar);
		 store.initcompleted();
		
		final BaseGrid grid = new BaseGrid(store, cm);  
		 
		  
		    grid.setLoadMask(true);  
		    grid.setBorders(true);  
		//    grid.setAutoExpandColumn("cname");  
		/*    ContentPanel searchpanel = new ContentPanel();
		    searchpanel.setCollapsible(true);  
		    searchpanel.setAnimCollapse(false);  
		    searchpanel.setExpanded(false);
		    TextField search = new TextField();  
		    search.setName("search");  
		    search.setFieldLabel("高级查找");  
		    search.setToolTip("高级查找，可能会花费你更长的时间.");
		    searchpanel.add(search);  */
		    BaseAdvSearch ba = new BaseAdvSearch();
		    ba.bindBaseStore(store);
		    
		    ContentPanel panel = new ContentPanel();  
		    panel.setFrame(true);  
		   
		  
		    panel.setButtonAlign(HorizontalAlignment.CENTER);  
		    panel.setIconStyle("icon-table");  
		    panel.setHeading("Paging Grid");  
		    panel.setLayout(new FlowLayout());  
		    
		    panel.setSize(600, 350);  
		 //   grid.setSize(600, 350);
		    grid.setAutoHeight(true);
		    grid.setAutoWidth(true);
		    
		  //  grid.setWidth("100%");
//		    grid.setHeight("100%");
		   panel.add(ba.getAdvsearch(),new FlowData(1));
		    panel.add(grid,new FlowData(1));  
		   
		   panel.setBottomComponent(toolBar);  
		    
		  
		    
		   
		    
		    StoreFilterField<ModelData> field = new StoreFilterField<ModelData>() {  
		        @Override  
		        protected boolean doSelect(Store<ModelData> store, ModelData  parent, ModelData  record,  
		            String property, String filter) {  
		        	for(int i=0;i<type.getFieldCount();i++)
		        		{
		        		
		        	String name = (String)record.get(type.getField(i).getName());  
		  
		          if (name.indexOf(filter.toLowerCase()) != -1) {  
		            return true;  
		          }  
		        		}
		          return false;  
		        }  
		    
		        @Override  
		        protected void onFilter() {  
		          super.onFilter();  
		        
		          grid.getSelectionModel().select(0,false);  
		        }  
		    
		      };  
		      field.setToolTip("对当前页面进行查找");
		      field.bind(store);  
		      field.setFieldLabel("当页搜索");
		      panel.setTopComponent(field);
		   return panel;
	}

	public ModelType getType() {
		return type;
	}

	public void setType(ModelType type) {
		this.type = type;
	}

	public List<ColumnConfig> getConfigs() {
		return configs;
	}

	public void setConfigs(List<ColumnConfig> configs) {
		this.configs = configs;
	}

	public StoreInfo getInfo() {
		return info;
	}

	public void setInfo(StoreInfo info) {
		this.info = info;
	}

	public BaseStore getStore() {
		return store;
	}

	public void setStore(BaseStore store) {
		this.store = store;
	}
	
	
/*	
	public  BasePagingLoader< PagingLoadConfig,  PagingLoadResult<ModelData>> getBaseLoader() {
		Date date = new Date();
		System.out.println("客户端发送数据请求=="+date.getTime()/1000+"--");
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
				"BaseServlet?Action=Loaddata"+"&tableinftablename=ccode&tableinfsql=select cname from ccode");
		//System.out.println("storeinfo="+storeinfo.getStoreInfoString());
		builder.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");  
	
		builder.setCallback(new RequestCallback(){

			@Override
			public void onError(Request request, Throwable exception) {
				// TODO Auto-generated method stub
				System.out.println("error.....");
			}

			@Override
			public void onResponseReceived(Request request, Response response) {
				// 
				System.out.println(response.getText().length());
				Date date = new Date();
				System.out.println("store在callback 中获得到数据=="+date.getTime()/1000);
			      
				
			}});
		HttpProxy proxy = new HttpProxy(builder);
	
			

		
		// need a loader, proxy, and reader
		XmlReader reader = new XmlReader(type)
		{
			@Override 
			protected ListLoadResult newLoadResult(Object loadConfig, List models) { 
					PagingLoadConfig pagingConfig = (PagingLoadConfig) loadConfig; 
					PagingLoadResult <ModelData> result = new BasePagingLoadResult<ModelData>(models, 
					pagingConfig.getOffset(), pagingConfig.getLimit()); 
					
					// result.setOffset(2);
					return result; 
					} 
					
			
		};
		
		
	//	BasePagingLoader<PagingLoadResult<ModelData>> loader = new BasePagingLoader<PagingLoadResult<ModelData>>(proxy,reader); 

		 BasePagingLoader< PagingLoadConfig,  PagingLoadResult<ModelData>>  loader = new BasePagingLoader< PagingLoadConfig,  PagingLoadResult<ModelData>> (proxy,reader);  
		// BaseListLoader loader = new BaseListLoader(proxy, reader);
		 Date date2 = new Date();
			System.out.println("客户端发送数据请求结束=="+date2.getTime()/1000);
			
//		 BaseStore<JDBCModelData> store = new BaseStore<JDBCModelData>(
//				loader);
		return loader;
	}*/
}
